package org.springframework.samples.petclinic.validation

import org.springframework.util.StringUtils
import org.springframework.validation.Errors

import org.springframework.samples.petclinic.Pet

class PetValidator {

  def validate(pet:Pet, errors:Errors) = {
    val name = pet.name
    if (!StringUtils.hasLength(name)) {
      errors.rejectValue("name", "required", "required");
    }
    else if (pet.isNew && pet.owner.getPet(name, true) != null) {
      errors.rejectValue("name", "duplicate", "already exists");
    }
  }
}

